<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>宣教助手</title>
  </head>
  <body>
    <style>
      html,
      body {
        height: 100vh;
      }
      body {
        background-image: linear-gradient(180deg, #bef0ee, #f5fffe 37%, #bff0ee);
      }
      .loading-box {
        position: fixed;
        text-align: center;
        width: 100%;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
      }
      .loading-text {
        color: #666;
        font-size: 16px;
        letter-spacing: 2px;
        margin-top: 10px;
        display: none;
      }
      .lds-default {
        display: inline-block;
        position: relative;
        width: 80px;
        height: 80px;
      }
      .lds-default div {
        position: absolute;
        width: 6px;
        height: 6px;
        background: #60d5d7;
        border-radius: 50%;
        animation: lds-default 1.2s linear infinite;
      }
      .lds-default div:nth-child(1) {
        animation-delay: 0s;
        top: 37px;
        left: 66px;
      }
      .lds-default div:nth-child(2) {
        animation-delay: -0.1s;
        top: 22px;
        left: 62px;
      }
      .lds-default div:nth-child(3) {
        animation-delay: -0.2s;
        top: 11px;
        left: 52px;
      }
      .lds-default div:nth-child(4) {
        animation-delay: -0.3s;
        top: 7px;
        left: 37px;
      }
      .lds-default div:nth-child(5) {
        animation-delay: -0.4s;
        top: 11px;
        left: 22px;
      }
      .lds-default div:nth-child(6) {
        animation-delay: -0.5s;
        top: 22px;
        left: 11px;
      }
      .lds-default div:nth-child(7) {
        animation-delay: -0.6s;
        top: 37px;
        left: 7px;
      }
      .lds-default div:nth-child(8) {
        animation-delay: -0.7s;
        top: 52px;
        left: 11px;
      }
      .lds-default div:nth-child(9) {
        animation-delay: -0.8s;
        top: 62px;
        left: 22px;
      }
      .lds-default div:nth-child(10) {
        animation-delay: -0.9s;
        top: 66px;
        left: 37px;
      }
      .lds-default div:nth-child(11) {
        animation-delay: -1s;
        top: 62px;
        left: 52px;
      }
      .lds-default div:nth-child(12) {
        animation-delay: -1.1s;
        top: 52px;
        left: 62px;
      }
      @keyframes lds-default {
        0%,
        20%,
        80%,
        100% {
          transform: scale(1);
        }
        50% {
          transform: scale(1.5);
        }
      }

      .modal {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transform: scaleX(1.1) scaleY(1.1);
        transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
        font-family: sans-serif;
      }
      .modal-content {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: white;
        padding: 1rem 1.5rem;
        width: 24rem;
        border-radius: 0.5rem;
      }
      .close-button {
        float: right;
        width: 1.5rem;
        line-height: 1.5rem;
        text-align: center;
        cursor: pointer;
        border-radius: 0.25rem;
        background-color: lightgray;
      }
      .close-button:hover {
        background-color: darkgray;
      }
      .show-modal {
        opacity: 1;
        visibility: visible;
        transform: scaleX(1) scaleY(1);
        transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
      }
      @media only screen and (max-width: 50rem) {
        h1 {
          color: #2a2a2a;
          font-size: 1.2rem;
          text-align: center;
        }
        .modal-content {
          width: calc(100% - 5rem);
        }
      }
    </style>
    <div class="loading-box">
      <div class="lds-default">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
      <div class="loading-text">加载中...</div>
    </div>

    <div class="modal">
      <div class="modal-content">
        <h1>微信授权失败，发生未知错误<br />请退出重新授权</h1>
      </div>
    </div>
    <script>
      function getAllUrlParams(url) {
        var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
        var obj = {};
        if (queryString) {
          queryString = queryString.split('#')[0];
          var arr = queryString.split('&');
          for (var i = 0; i < arr.length; i++) {
            var a = arr[i].split('=');
            var paramName = a[0];
            var paramValue = typeof a[1] === 'undefined' ? true : a[1];
            if (paramName.match(/\[(\d+)?\]$/)) {
              var key = paramName.replace(/\[(\d+)?\]/, '');
              if (!obj[key]) obj[key] = [];
              if (paramName.match(/\[\d+\]$/)) {
                var index = /\[(\d+)\]/.exec(paramName)[1];
                obj[key][index] = paramValue;
              } else {
                obj[key].push(paramValue);
              }
            } else {
              if (!obj[paramName]) {
                obj[paramName] = paramValue;
              } else if (obj[paramName] && typeof obj[paramName] === 'string') {
                obj[paramName] = [obj[paramName]];
                obj[paramName].push(paramValue);
              } else {
                obj[paramName].push(paramValue);
              }
            }
          }
        }
        return obj;
      }

      function skip(path) {
        window.localStorage.removeItem('redirect_uri');
        window.location.href = path;
      }

      function getWxAuthUrl(params = {}) {
        return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${params.appId}&redirect_uri=${window.location.href}&response_type=code&scope=snsapi_userinfo&state=STATE&component_appid=${params.componentAppId}#wechat_redirect`;
      }

      function getApiOrigin() {
        return window.location.origin.indexOf('-exp') !== -1
          ? 'https://xxx.cn'
          : 'https://xxx.cn';
      }

      function setup() {
        let urlParams = getAllUrlParams();
        let redirect_uri = window.localStorage.getItem('redirect_uri') || '';
        if (!redirect_uri) window.localStorage.setItem('redirect_uri', urlParams.targetPage);

        if (!urlParams.needAuth && !urlParams.code) {
          return skip(decodeURIComponent(redirect_uri));
        }

        if (urlParams.code) {
          fetch(
            `${getApiOrigin()}/hos/platformUserOffiAccount/sns/auth?appId=${urlParams.appId}&code=${
              urlParams.code
            }`
          )
            .then((res) => res.json)
            .then((res) => {
              skip(decodeURIComponent(redirect_uri));
            })
            .catch(() => {
              toggleModal();
            });
        } else {
          window.location.href = getWxAuthUrl(urlParams);
        }
      }

      setup();

      function toggleModal() {
        const modal = document.querySelector('.modal');
        modal.classList.add('show-modal');
      }
    </script>
  </body>
</html>
